﻿using SmallJob.Model;
using SmallJob.Services.Implements;
using SmallShop.Services.Implements;
using System;

namespace SmallShop.Services.Code
{
    public class OrderHelper
    {
        public static int GetPayerID(Order order)
        {
            switch (order.Type)
            {
                case (int)OrderType.PROVIDE:
                    return order.BuyerID;

                case (int)OrderType.REQUEST:
                    return order.BuyerID;

                default: return 0;
            }
        }

        public static int GetWorkerID(Order order)
        {
            switch (order.Type)
            {
                case (int)OrderType.PROVIDE:
                    return order.SellerID;

                case (int)OrderType.REQUEST:
                    return order.SellerID;

                default: return 0;
            }
        }

        public static Boolean SetPersonForOrder(Order order, int? payerid, int? workerid)
        {
            switch (order.Type)
            {
                case (int)OrderType.PROVIDE:
                    if (payerid != null) order.BuyerID = (int)payerid;
                    if (workerid != null) order.SellerID = (int)workerid;
                    return true;

                case (int)OrderType.REQUEST:
                    if (payerid != null) order.BuyerID = (int)payerid;
                    if (workerid != null) order.SellerID = (int)workerid;
                    return true;

                default: return false;
            }
        }

        public static Boolean TestIsWorker(MConversation cvs, int testid)
        {
            switch (cvs.Type)
            {
                case (int)MessageType.ORDER_PROVIDE:
                    return cvs.Order.Provide.HostID == testid;

                case (int)MessageType.ORDER_REQUEST:
                    return cvs.Order.Request.HostID == testid;

                default: return false;
            }
        }

        public static Boolean IsOrderCanCancel(Order order)
        {
            if (order.Status == (int)OrderStatusV2.WaitProgress) return true;
            return false;
        }

        public static Boolean IsNeedPrePayment(int amount, Provide provide)
        {
            if ((provide == null || !provide.IsDeliveryImmediate) && amount > 1000000)
                return true;
            return false;
        }
        public static int GetAmountPrePayment(int total, Provide provide)
        {
            if (IsNeedPrePayment(total, provide))
            {
                var get20 = total / 100 * 20;
                var temp = get20 % 1000;
                var result = (get20 - temp);
                return result;
            }
            else
            {
                return total;
            }

        }

        public static bool TestCanDownload(Order order, int p)
        {
            if (order == null) return false;
            if (GetPayerID(order) == p) return true;
            return false;
        }
    }
}